<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/15
 * Time: 17:56
 */

namespace app\admin\model;


class PacketRule extends DataBase
{
    protected $name = 'packet_rule';
    protected $autoWriteTimestamp=true;

    public function getDataList()
    {
        $page_config = array(
            'page' => input('get.offset',1),
            'list_rows' => input('get.limit',100)
        );
        $data = $this->paginate($page_config);
        foreach($data as $key=>$value){
            $data[$key]['begin'] = date('H:i:s',strtotime(date('Y-m-d'))+$value['begin']);
            $data[$key]['end'] = date('H:i:s',strtotime(date('Y-m-d'))+$value['end']);
        }

        return $data;
    }

    public function packetRuleAdd()
    {
        $newData = input('post.');
        $newData['begin'] = strtotime($newData['begin'])-strtotime(date('Y-m-d',strtotime($newData['begin'])));
        $newData['end'] = strtotime($newData['end'])-strtotime(date('Y-m-d',strtotime($newData['end'])));
        $newData['type'] = 2;
        $res= $this->data($newData)->save();
        if($res) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

    public function packetRuleChangeStatus()
    {
        $id = input('get.id');

        $status = $this->where('id','=',$id)->value('status');
        $status = $status ? 0 : 1;
        $res = $this->save(['status'=>$status],['id'=>$id]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function packetRuleEdit()
    {
        $newData = input('get.');
        $newData['begin'] = strtotime($newData['begin'])-strtotime(date('Y-m-d',strtotime($newData['begin'])));
        $newData['end'] = strtotime($newData['end'])-strtotime(date('Y-m-d',strtotime($newData['end'])));
        $res = $this->save($newData,['id'=>$newData['id']]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }
}